import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * Created whit IntelliJ IDEA.
 * Description：
 * User：ZHONGCHEN
 * Date:2022-04-08
 * Time:12:06
 */
class Node {
    public int val;
    public List<Node> children;

    public Node() {
    }

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};

public class TestDemo {
    //429. N 叉树的层序遍历
    //给定一个 N 叉树，返回其节点值的层序遍历。（即从左到右，逐层遍历）。
    //树的序列化输入是用层序遍历，每组子节点都由 null 值分隔（参见示例）。
    public static List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ans = new ArrayList<>();
        Queue<Node> queue = new ArrayDeque<>();
        if (root == null) {
            return ans;
        } else {
            queue.offer(root);
        }
        while (!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int cnt = queue.size();
            for (int i = 0; i < cnt; i++) {
                Node cur = queue.poll();
                list.add(cur.val);
                for (Node child : cur.children) {
                    queue.offer(child);
                }
            }
            ans.add(list);
        }
        return ans;
    }
    
}
